import requestMeeting, { request } from '@/plugins/axios'
import { IGetMeetingInfo, IList } from './interface'
import { MtEntity } from '../mt'
import { OrgTreeEntity } from '@/pages/manager/blocks/main/organizeTree/translator'

export const fetchAllMts = async (meetingMoid: string) =>
	request<IList>({
		url: `/mcc/${meetingMoid}/mts/all_with_conf`,
		method: 'get',
	}).then((res) => (res.success && res.list ? res.list : undefined))

export const fetchAllMccMts = async (meetingMoid: string) =>
	request<IList>({
		url: `/mcc/${meetingMoid}/mts/all`,
		method: 'get',
	}).then((res) => (res.success && res.list ? res.list : undefined))

export const fetchGetMccOrg = async (meetingMoid: string) =>
	request<{ list: OrgTreeEntity[] }>({
		url: `/mcc/${meetingMoid}/org`,
		method: 'get',
	}).then((res) => (res.success && res.list ? res.list : []))

export const fetchGetMeetingInfo = async (meetingMoid: string) =>
	requestMeeting<IGetMeetingInfo>({
		url: `/mcc/${meetingMoid}`,
		method: 'get',
	}).then((res) => res)

export const fetchMonitor = async () =>
	requestMeeting<{ monitor_api_address: string }>({
		url: `/monitor_api_address`,
		method: 'get',
	}).then((res) =>
		res.success && res.data && res.data.monitor_api_address ? res.data.monitor_api_address : undefined
	)

export const fetchJdb = async (meetingMoid: string) =>
	requestMeeting<0 | 1>({
		url: `/jdb_status/${meetingMoid}`,
		method: 'get',
	}).then((res) => (res.success && res.data ? 1 : 0))

export const fetchMixMode = async (meetingMoid: string) =>
	requestMeeting<{ mode?: 0 | 2 | 1 }>({
		url: `/mcc/${meetingMoid}/mixs/1`,
		method: 'get',
	}).then((res) => (res.success && res.data ? res.data.mode ?? 0 : 0))

export const fetchCascadeNodes = async ({ meetingMoid, id }: { meetingMoid: string; id: string }) =>
	request<{ list: MtEntity[] }>({
		url: `/mcc/${meetingMoid}/cascades/${id}/mts`,
		method: 'get',
	}).then((res) => (res.success && res.list ? res.list : undefined))

/** 是否有下级会议 */
export const fetchSubMeeting = async (meetingMoid: string) =>
	requestMeeting<{ level_meeting: string }>({
		url: `/mcc/${meetingMoid}/sub_meeting`,
		method: 'get',
	}).then((res) => res)

/** 结束下级会议 */
export const fetchEndSubconf = async (meetingMoid: string) =>
	requestMeeting({
		url: `/confs/${meetingMoid}?end_subconf=1`,
		method: 'DELETE',
	}).then((res) => res)
